package com.simple.space.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.simple.space.config.datasource.CommonDataSourceConfig;

import java.sql.SQLException;

/**
 * <p>
 *     <br/>数据源工具类
 * </p>
 */
public final class DataSourceUtil {

	private DataSourceUtil(){}

	public static DruidDataSource getDataSource(String url, String user, String password, String driverClassName,
			String dbType, CommonDataSourceConfig config) {

		DruidDataSource datasource = new DruidDataSource();
		datasource.setUrl(url);
		datasource.setUsername(user);
		datasource.setPassword(password);
		datasource.setDriverClassName(driverClassName);
		datasource.setDbType(dbType);

		datasource.setInitialSize(config.getInitialSize());
		datasource.setMinIdle(config.getMinIdle());
		datasource.setMaxActive(config.getMaxActive());
		datasource.setMaxWait(config.getMaxWait());
		datasource.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis());
		datasource.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
		datasource.setValidationQuery(config.getValidationQuery());
		datasource.setTestWhileIdle(config.isTestWhileIdle());
		datasource.setTestOnBorrow(config.isTestOnBorrow());
		datasource.setTestOnReturn(config.isTestOnReturn());
		datasource.setPoolPreparedStatements(config.isPoolPreparedStatements());
		datasource.setMaxPoolPreparedStatementPerConnectionSize(config.getMaxPoolPreparedStatementPerConnectionSize());

		try {
			datasource.setFilters(config.getFilters());
		} catch (SQLException e) {
		}
		datasource.setConnectionProperties(config.getConnectionProperties());
		return datasource;
	}
}
